<?php
function getPostSpellCheck($target) {
	global $blogURL, $pluginURL;
	$script[] = '<script type="text/javascript">var blogURL_USC="'.$blogURL.'";</script>';
	$script[] = '<script type="text/javascript" src="'.$pluginURL.'/lib/spellcheck.js"></script>';
	$script[] = '<script language="javascript" type="text/javascript">';
	$script[] = '//<![CDATA[';
	$script[] = 'String.prototype.trim = function () {return this.replace(/\s\s+/, " ").replace("\'", "").replace(/^\s*/g, "").replace(/\s*$/g, "");}';
	$script[] = 'function spellcheck_owner(caller) {';
	$script[] = '	if(editor.editMode == "WYSIWYG") {';
	$script[] = '		var contentDoc = editor.contentDocument.body;';
	$script[] = '		var text = (document.all) ? contentDoc.innerText : contentDoc.textContent;';
	$script[] = '	} else {';
	$script[] = '		var text = document.getElementById("editWindow").value;';
	$script[] = '	}';
	$script[] = '	var content = text.trim();';
	$script[] = '	getSpellcheck("", "", content, 0);';
	$script[] = '}//]]></script>';
	$script[] = '<div id="SpellView" style="position:absolute;display:none;z-index:200;width:600px;height:400px;overflow-y:scroll;"></div>';
  	$script[] = '<img src="'.$pluginURL.'/lib/spellcheck_icon.gif" width="74" height="20" border="0"  onclick="spellcheck_owner(this);return false;" style="cursor:pointer;">';

	return implode("\r\n", $script).$target;
}

function getCommentSpellCheck($target) {
	global $pluginURL;
	$target .= '<img src="'.$pluginURL.'/lib/spellcheck_icon.gif" width="74" height="20" border="0" align="absbottom" onclick="getSpellcheck(this, \'[##_article_rep_id_##]\', \'\', 1);return false;" style="cursor:pointer;">&nbsp;(<a href="http://urimal.cs.pusan.ac.kr/urimal_new" onclick="window.open(this.href); return false;">우리말 배움터</a>)';
	return $target;
}

function getSpellCheckScript($target) {
	global $blogURL, $pluginURL;
	ob_start();
?>
<script type="text/javascript">var blogURL_USC="<?php echo $blogURL;?>";</script>
<script type="text/javascript" src="<?php echo $pluginURL;?>/lib/spellcheck.js"></script>
<?php
	$target .= ob_get_contents();
	ob_end_clean();
	return $target;
}

function getSpellProcess($target){
	global $configVal;
	require_once('lib/getIconv.php');
	requireComponent('Eolin.PHP.HTTPRequest');
	requireComponent('Textcube.Function.misc');
	$data = misc::fetchConfigVal($configVal);

	if(is_null($data)){
		$data['actionURL']	= "http://164.125.36.47/WebSpell_ISAPI.dll?Check";
	}

	$spellHost = $data['actionURL'];
	$spellBody = $_POST['content'];
	$spellBody = stripslashes($spellBody);
	$spellBody = getIconv($spellBody, "EUC-KR");
	$spellBody = preg_replace('/\s\s+/', ' ', trim($spellBody));

	$request = new HTTPRequest('POST', $spellHost);
	$request->contentType = 'application/x-www-form-urlencoded; charset=euc-kr';
	$request->send("&text1=".$spellBody);
	$result = $request->responseText;

	if($result){
		preg_match("@<style.*?</style>@isU", $result, $style);
		$tempStyle = str_replace("td {", ".tdStyle {", $style[0]);
		if(preg_match("@<table border=1.*?</table><br>@isU", $result, $table)){
			$tempTable = preg_replace('@<a href=.*">@isU', '', $table[0]);
			$tempTable = str_replace(array("width=410","width=50","width=220","<td>","e><br>","<td"), array("width=550","width=100","","<td width=60>","e>","<td class='tdStyle' "), $tempTable);
		}else{
			preg_match("@<font size=2>.*?</font>@isU", $result, $table);
			$tempTable ="<div style='background-color:#ffffff;text-align:center;margin-top:30px;font:9pt dotum;'>".$table[0]."</div>";
		}
		$spellValue = $tempStyle.$tempTable;
	}else{
		$spellValue = "<div style='background-color:#ffffff;text-align:center;margin-top:30px;font:9pt dotum;'>'" . $spellHost . "' Connection it failed.</div>";
	}
	header('Content-Type: text/xml; charset=EUC-KR');
	echo '<?xml version="1.0" encoding="euc-kr"?><response><error>0</error><spellContent><![CDATA[', $spellValue, ']]></spellContent></response>';
}

function getSpellCheckDataSet($DATA){
	requireComponent('Textcube.Function.misc');
	$cfg = misc::fetchConfigVal($DATA);
	if(!$cfg['actionURL']) return "::입력 오류::\n\n맞춤법/문법 검사 관련 URL은 모두 입력하셔야 합니다.";
	return true;
}
?>